<?php
/* 
    � Alfa CMS Version 1.0.0.1 �
    � Habbo Content Management System - Alfa CMS create by JG �
    � This CMS is for private use, distribution or marketing prohibited. �
    � Jg Projects - Jg Webmaster [http://jgwebmaster.com.ar] �
    � Images and styles from Habbo hotel Sulake, All rights reserved. �
*/

require_once '../Environment.php';

RequireLogin();
checkRegister(2);

$_SESSION['quickregister']['step_timesmap'] = time();

CleanVars();

//data 
$data = [
    $_POST['bean_email'],
    $_POST['bean_retypedEmail'],
    $_POST['bean_password'],
    $_POST['bean_termsOfServiceSelection'],
    $_POST['bean_marketing']
];

$response = $_POST['recaptcha_response_field'];
$captcha_field = $_POST['recaptcha_challenge_field'];

$_SESSION['quickregister']['step2']['bean_email'] = $data[0];
$_SESSION['quickregister']['step2']['bean_retypedEmail'] = $data[1];
$_SESSION['quickregister']['step2']['bean_password'] = $data[2];
$_SESSION['quickregister']['step2']['bean_termsOfServiceSelection'] = $data[3];
$_SESSION['quickregister']['step2']['bean_marketing'] = $data[4]; 

$error = false;
$num_e = false;

unset($_SESSION['quickregister']['error']);
unset($_SESSION['quickregister']['step2']['error']);

$errors = [
    // password
     '03x' => 
         [
             '%step2_error_03_1%',
             '%step2_error_03_2%',
             '%step2_error_03_3%'
         ],
    // terms
    '06x' => '%step2_error_06_1%',
    // email
    '07x' => '%step2_error_07_1%',
    // retry email
    '09x' => 
        [
            '%step2_error_09_1%',
            '%step2_error_09_2%',
            '%step2_error_09_3%'
        ],

    '12x' => '%captcha_error%'
];

if($_SESSION['login']['error_captcha'] >= 4)
{
    if(!is_captcha($captcha_field, $response))
    {
        $num_e = $num_e . '12x';
        $e = $e . $errors['12x'];
        goto error;
    }
}

if(!empty($data[0]) && !empty($data[1]))
{
    if($data[0] == $data[1])
    {
        $query = query("SELECT null FROM users WHERE email = '" . $data[0] . "' LIMIT 1");
        $count = mysql_num_rows($query);
        
        if($count > 0)
        {
            unset($_SESSION['quickregister']['step2']['error']);
            $e = $e . $errors['09x'][2];
            goto duplicate;
        }
    }
}

if(empty($data[2]))
{
    $num_e = $num_e . '03x';
    $e = $e . $errors['03x'][0];
    $_SESSION['quickregister']['step2']['error']['password'] = 'class="error"';
    $_POST = TRUE;
    checkFocus();
    $e_ = true;

}
else if(strlen($data[2]) < 6) 
{
    $error = TRUE;
    $num_e = $num_e . '03x';
    $e = $e . $errors['03x'][1];
    $_SESSION['quickregister']['step2']['error']['password'] = 'class="error"';
    $_POST = TRUE;
    checkFocus();
    $e_ = true;
}
else if (!preg_match('`[0-9]`', $data[2]))
{
    $error = TRUE;
    $num_e = $num_e . '03x';
    $e = $e . $errors['03x'][2];
    $_SESSION['quickregister']['step2']['error']['password'] = 'class="error"';
    $_POST = TRUE;
    checkFocus();
    $e_ = true;
}

if(empty($data[3]))
{
    $error = TRUE;
    $num_e = $num_e . '06x';
    $e = $e . $errors['06x'];
    $_SESSION['quickregister']['step2']['error']['terms'] = 'error';
    $e_ = true;
}

if(empty($data[1])) 
{
    $error = TRUE;
    $num_e = $num_e . '09x';
    $e = $e . $errors['09x'][1];
    $_SESSION['quickregister']['step2']['error']['rmail'] = 'class="error"';
    $r = TRUE;
    checkFocus();
    $e_ = true;
} 
else if($data[1] !== $data[0])
{
    $error = TRUE;
    $num_e = $num_e . '09x';
    $e = $e . $errors['09x'][2];
    $_SESSION['quickregister']['step2']['error']['rmail'] = 'class="error"';
    $r = TRUE;
    checkFocus();
    $e_ = true;
}

if(empty($data[0]) || !$core->validateMail($data[0])) 
{
    $error = TRUE;
    $num_e = $num_e . '07x';
    $e = $e . $errors['07x'];
    $_SESSION['quickregister']['step2']['error']['mail'] = 'class="error"';
    $m = TRUE;
    checkFocus();
    $e_ = true;
}

function checkFocus()
{
    if($_POST || $m || $r)
    {
        $focus = 'register-password';
    }
    else if(!$_POST || $m || $r)
    {
        $focus = 'email-address2';
    }
    else if(!$_POST || !$m || $r)
    {
        $focus = 'email-address';
    }
     else 
    {
        $focus = '';
    }
}

if($e_)
{
    goto error;
}

$_SESSION['quickregister']['step2']['complete'] = true;
header('Location: ' . WWW . '/quickregister/captcha');
exit();

error:
{
    $_SESSION['quickregister']['error'] = utf8_encode($e);
    $_SESSION['quickregister']['step2']['error']['focus'] = $focus;
    Header('Location: ' . WWW . '/quickregister/email_password/e/' . $num_e);
    exit();
}

duplicate:
{
    Location('quickregister/duplicateEmailLogin?next=/identity/add_avatar');
}
?>